﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WeChat.Common;
using System.Web.Script.Serialization;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using WeChat.HyfPay.Hyflib;
using BLL;
using Models;
namespace WeChat.HyfPay
{
    public partial class ResultPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
          
            notice_url();
        }
        //支付回调页面
        public void notice_url()
        {
            JObject jo = getPostStr();
            SortedDictionary<string, object> results = JsonConvert.DeserializeObject<SortedDictionary<string, object>>(jo.ToString());
            if (results.ContainsKey("respCode") && results["respCode"].ToString() == "00")
            {
                //验证签名，判断接收信息是否合法
                string getSign = results["sign"].ToString();
                string sign = HyfPayApi.toSign(results);
                if (!getSign.Equals(sign))
                {
                    ApplicationLog.Write("接收到非法信息！");
                    return;
                }
                try
                {
                   
                    HyfResultOrder ho = new HyfResultOrder();
                    ho.version = results["version"].ToString();
                    ho.hicardMerchNo = results["hicardMerchNo"].ToString();//商户号
                    ho.merchOrderNo = results["merchOrderNo"].ToString();//商户订单号        
                    ho.payType = results["payType"].ToString();//支付类型
                    ho.amount = (Double.Parse(results["amount"].ToString()) / 100).ToString();//支付金额
                    ho.hicardOrderNo = results["hicardOrderNo"].ToString();//汇耘富支付订单号
                    ho.createTime = results["createTime"].ToString();//订单生成时间
                    ho.payTime = results["payTime"].ToString();//支付时间
                    ho.cardno = results["remark"].ToString();//卡号
                    ho.respCode = results["respCode"].ToString();//返回码
                    ho.respMsg = results["respMsg"].ToString();//返回码说明
                    ho.buyerId = results["buyerId"].ToString();//用户标识
                    ho.isCredit = results["isCredit"].ToString();//借贷标识
                    ho.channelOrderNo = results["channelOrderNo"].ToString();//渠道订单号
                    ho.sign = results["sign"].ToString();//签名
                    int i = HyfAddBLL.addRecord(ho);
                  
                }
                catch (Exception ex)
                {
                    ApplicationLog.Write("插入失败：" + ex.Message);
                }
            }
            Response.Write("SUCCESS");
            Response.End();
        }

        /// <summary>
        /// 接收汇耘富服务器发送的json数据
        /// </summary>
        /// <returns></returns>
        public JObject getPostStr()
        {
            Stream stream = Request.InputStream;
            int len = (int)stream.Length;
            byte[] inputbytes = new byte[len];
            stream.Read(inputbytes, 0, len);
            stream.Close();
            string data = Encoding.GetEncoding("utf-8").GetString(inputbytes);
            ApplicationLog.Write("汇耘富回调地址接收数据："+data);
            return JObject.Parse(data);
        }

      
    }
}